package com.tanhua.api.mongo;


import com.mongodb.client.result.DeleteResult;
import com.tanhua.mongo.Comment;
import com.tanhua.mongo.Publish;
import com.tanhua.vo.PageBeanVo;
import org.apache.dubbo.config.annotation.Service;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;

@Service
public class CommentApiImpl implements CommentApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public Integer save(Comment comment) {

        //根据动态id查询动态发布人的id
        Publish publish = mongoTemplate.findById(comment.getPublishId(), Publish.class);
        comment.setPublishUserId(publish.getUserId()); //发布动态人的id
        mongoTemplate.insert(comment);

        //修改public动态表（累加点赞评论或者喜欢行为）
        Query query = new Query(Criteria.where("id").is(comment.getPublishId()));
        Update update = new Update();
        String type = comment.getType();//行为类型
        update.inc(type,1);
        mongoTemplate.upsert(query,update, Publish.class);

        //查询点赞、喜欢、评论数
        Query pubTypeQuery = new Query(Criteria.where("pubType").is(comment.getPubType()).and("commentType").is(comment.getCommentType()).and("publishId").is(comment.getPublishId()));
        long count = mongoTemplate.count(pubTypeQuery, Comment.class);
        return Math.toIntExact(count);

    }

    @Override
    public Integer remove(Comment comment) {
        Query query = new Query(
                Criteria.where("publishId").is(comment.getPublishId())
                        .and("commentType").is(comment.getCommentType())
                        .and("userId").is(comment.getUserId())
        );
        //删除记录表
        DeleteResult remove = mongoTemplate.remove(query, Comment.class);
        //修改publish表
        Query pQuery = new Query(Criteria.where("id").is(comment.getPublishId()));
        Update update = new Update();
        String type = comment.getType();//类型（点赞，喜欢，评论）
        update.inc(type,-1);
        mongoTemplate.upsert(pQuery,update,Publish.class);
        //查询点赞、喜欢、评论数。
        Query countQuery = new Query(Criteria.where("pubType").is(comment.getPubType())
                .and("commentType").is(comment.getCommentType()).and("publishId").is(comment.getPublishId()));
        Long count = mongoTemplate.count(countQuery, Comment.class);
        return count.intValue();
    }

    @Override
    public PageBeanVo findCommentByPage(Integer pageNum, Integer pageSize,String publishId) {

        // 分页查询评论列表
        Query query = new Query(Criteria.where("publishId").is(new ObjectId(publishId))
                                        .and("pubType").is(1).and("commentType").is(2));
        query.with(new Sort(Sort.Direction.DESC,"created"));
        query.skip((pageNum-1)*pageSize).limit(pageSize);
        List<Comment> list = mongoTemplate.find(query, Comment.class);
        long count = mongoTemplate.count(query, Comment.class);
        PageBeanVo pageBeanVo = new PageBeanVo(pageNum,pageSize,count,list);
        return pageBeanVo;
    }

    @Override
    public PageBeanVo findPage(Integer pageNum, Integer pageSize, int commentType, Long userId) {
        // 1、分页条件查询
        Integer index = (pageNum-1)*pageSize;

        Query query = Query.query(Criteria.where("commentType").is(commentType).and("publishUserId").is(userId))
                .skip(index).limit(pageSize);

        List<Comment> commentList = mongoTemplate.find(query, Comment.class);

        long counts = mongoTemplate.count(query, Comment.class);

        // 2、返回分页对象
        return new PageBeanVo(pageNum, pageSize, counts, commentList);
    }

    @Override
    public Integer update(String commentId) {
        Comment comment = mongoTemplate.findById(new ObjectId(commentId), Comment.class);
        Query query = new Query(Criteria.where("id").is(new ObjectId(commentId)));
        Update update = new Update();
        update.inc("likeCount",1);
        mongoTemplate.upsert(query,update,Comment.class);
        Comment comment1 = mongoTemplate.findById(commentId, Comment.class);
        return comment1.getLikeCount();
    }

    @Override
    public Integer delete(String commentId) {
        Comment comment = mongoTemplate.findById(new ObjectId(commentId), Comment.class);
        Query query = new Query(Criteria.where("id").is(new ObjectId(commentId)));
        Update update = new Update();
        update.inc("likeCount",-1);
        mongoTemplate.upsert(query,update,Comment.class);
        Comment comment1 = mongoTemplate.findById(commentId, Comment.class);
        return comment1.getLikeCount();
    }

}
